package com.jackrain.nea.common;


import com.alibaba.fastjson.JSONObject;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.core.redis.RedisObjectSerializer;
import com.jackrain.nea.web.query.DefaultWebEvent;
import com.jackrain.nea.web.query.QuerySessionImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.net.URI;

@Service
public class PostHandleEvent {


    @Autowired
    private PropertiesConf pconf;
//
//    @Autowired
//    private RestTemplateService restTemplateService;

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    private RedisObjectSerializer serializer = new RedisObjectSerializer();


    public JSONObject executePostHandle(JSONObject jo, QuerySessionImpl userquerySession, DefaultWebEvent event, String action) throws Exception {

        String fcedas = pconf.getProperty("fc.edas.url", "");
        URI uri = new URI(fcedas + "/fc/api/exehandlecommand");

        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();

        JSONObject paramObject = new JSONObject();
        paramObject.put("action", action);
        paramObject.put("jo", jo.toJSONString());

        byte[] eventB = serializer.serialize(event);
        byte[] userquerySessionB = serializer.serialize(userquerySession);

        paramObject.put("event", new String(eventB, "ISO-8859-1"));
        paramObject.put("querySession", new String(userquerySessionB, "ISO-8859-1"));

        map.add("param", paramObject.toJSONString());
        String retStr = restTemplateUtil.doPostForm(uri, map);
        JSONObject retJo = JSONObject.parseObject(retStr);

        return retJo;

    }


}
